package com.yc.springmvc.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

// @Controller 表示该类所有方法, 默认返回视图名
@Controller
public class TestController {

    @RequestMapping("goMypage")
    public String toMypage(){
        return "redirect:mypage.html";
    }

    @RequestMapping({"/**/sayHello", "say"})
    // @ResponseBody 表示该方法返回 json 格式数据
    @ResponseBody
    public String hello(){
        return "hello world!";
    }

    // 请求转发的方式跳转到 /WEB-INF/templates/th.html
    @RequestMapping("toth")
    public String toTh(String message, Model model){
        model.addAttribute("msg", message);
        return "th";
    }

    // 自定义参数映射
    // /sub?a=100
    @RequestMapping("sub")
    @ResponseBody
    public int sub(
            @RequestParam("a") int x,
            @RequestParam(value = "y",required = false,
                    defaultValue = "10") int y){
        return x - y;
    }

    // 地址参数
    // /add/10/20
    @RequestMapping("add/{x}/{z}")
    @ResponseBody
    public int add(
            @PathVariable int x,
            @PathVariable("z")int y){
        return x + y;
    }

    // 获取头域参数值
    @RequestMapping("headers")
    @ResponseBody
    public String headers(
            @RequestHeader("Host") String host,
            @RequestHeader String Accept,
            @CookieValue String name
    ){
        return host + " ; " + Accept + " ; " + name;
    }



}
